package com.leetcode.hot100;


import java.util.*;

/**
 * @BelongsProject: LeetCode
 * @BelongsPackage: com.leetcode.hot100
 * @Author: lei
 * @CreateTime: 2024-03-22  09:26
 * @Description: TODO
 * @Version: 1.0
 */
public class 字母异位词分组_49 {
    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        groupAnagrams(strs);
    }

    public static List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> hashMap = new HashMap<>();
        for (String str : strs) {
            String key = change(str);
            if (hashMap.containsKey(key)) {
                List<String> list = hashMap.get(key);
                list.add(str);
                hashMap.put(key, list);
            } else {
                List<String> list = new ArrayList<>();
                list.add(str);
                hashMap.put(key, list);
            }
        }
        return new ArrayList<>(hashMap.values());
    }

    private static String change(String str) {
        char[] chars = str.toCharArray();
        Arrays.sort(chars);
        return String.valueOf(chars);
    }
}

